<?php

require_once (SRC_DIR . '/framework/Request/Request.php');
require_once (SRC_DIR . '/framework/Request/URLMap.php');


class TestOfURLMap extends UnitTestCase	{
	public function testPathAddedAndRetrieved()	{
		$urlMap = new URLMap();
		
		$urlMap->addURL('/', 'index');
		
		$this->assertEqual($urlMap->getTargetNameFromPath('/'), 'index');
	}
	
	public function testGetPathFromRequest()	{
		$fakeServerData = array(
		  'HTTP_HOST' => 'www.foo.com',
		  'HTTP_USER_AGENT' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 3.5.30729)',
		  'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
		  'HTTP_ACCEPT_LANGUAGE' => 'en-gb,en;q=0.5',
		  'HTTP_ACCEPT_ENCODING' => 'gzip,deflate',
		  'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
		  'HTTP_KEEP_ALIVE' => '300',
		  'HTTP_CONNECTION' => 'keep-alive',
		  'HTTP_CACHE_CONTROL' => 'max-age=0',
		  'SERVER_SIGNATURE' => '',
		  'SERVER_SOFTWARE' => 'Apache/2.2.11 (Win32) PHP/5.2.9-2',
		  'SERVER_NAME' => 'www.foo.com',
		  'SERVER_ADDR' => '10.0.0.100',
		  'SERVER_PORT' => '80',
		  'REMOTE_ADDR' => '10.0.0.100',
		  'DOCUMENT_ROOT' => 'C:/wamp/www/',
		  'SERVER_ADMIN' => 'admin@localhost',
		  'SCRIPT_FILENAME' => 'C:/wamp/www/project_seraph-php-framework/unit_tests/allTests.php',
		  'REMOTE_PORT' => '3784',
		  'GATEWAY_INTERFACE' => 'CGI/1.1',
		  'SERVER_PROTOCOL' => 'HTTP/1.1',
		  'REQUEST_METHOD' => 'GET',
		  'QUERY_STRING' => '',
		  'REQUEST_URI' => '/test-url/',
		  'SCRIPT_NAME' => '/test-url/',
		  'PHP_SELF' => '/project_seraph-php-framework/unit_tests/allTests.php',
		  'REQUEST_TIME' => 1255152026,
		
		);
		$r = new Request('/', $fakeServerData);
		
		$map = new URLMap();
		$map->addURL('/', 'base');
		$map->addURL('/test-url/', 'testa');
		$map->addURL('/wibble', 'testc');
		
		$this->assertEqual($map->getTargetNameFromPath($r->getRequestedPath()), 'testa');
		
		//knock the trailing slash off the request
		$fakeServerData['REQUEST_URI'] = '/test-url';
		$r2 = new Request('/', $fakeServerData);
		$this->assertEqual($map->getTargetNameFromPath($r2->getRequestedPath()), 'testa');

		$fakeServerData['REQUEST_URI'] = '/wibble';
		$r3 = new Request('/', $fakeServerData);
		$this->assertEqual($map->getTargetNameFromPath($r3->getRequestedPath()), 'testc');
	}
	
	public function testURLContainsWildcard()	{
		$map = new URLMap();
		$this->assertTrue($map->URLContainsWildcard('/something/*'));
		$this->assertFalse($map->URLContainsWildcard('/something/'));
	}
	
	public function testWildCardPaths()	{
		$map = new URLMap();
		$map->addURL('test/*', 'Test1Wildcard');
		$map->addURL('*', 'Test2Wildcard');
		$map->addURL('test3', 'Test3Specific');
		$map->addURL('test/kaboom', 'Test4Kaboom');
		$map->addURL('test/kaboom/boom', 'Test5KaboomBoom');
		
		$this->assertEqual($map->getTargetNameFromPath('/test'), 'Test1Wildcard');
		$this->assertEqual($map->getTargetNameFromPath('/test/'), 'Test1Wildcard');
		$this->assertEqual($map->getTargetNameFromPath('/test/somethingrandom'), 'Test1Wildcard');
		$this->assertEqual($map->getTargetNameFromPath('/test/somethingrandom/'), 'Test1Wildcard');
		$this->assertEqual($map->getTargetNameFromPath('/test/somethingrandom/somethingelse'), 'Test1Wildcard');
		
		$this->assertEqual($map->getTargetNameFromPath('/jiggawhat'), 'Test2Wildcard');
		$this->assertEqual($map->getTargetNameFromPath('/'), 'Test2Wildcard');
		$this->assertEqual($map->getTargetNameFromPath('/hubba/hubba'), 'Test2Wildcard');
		
		$this->assertEqual($map->getTargetNameFromPath('/test3'), 'Test3Specific');
		$this->assertEqual($map->getTargetNameFromPath('/test3/'), 'Test3Specific');
		
		$this->assertEqual($map->getTargetNameFromPath('/test/kaboom'), 'Test4Kaboom');
		$this->assertEqual($map->getTargetNameFromPath('/test/kaboom/'), 'Test4Kaboom');
		$this->assertEqual($map->getTargetNameFromPath('/test/kaboom/boom'), 'Test5KaboomBoom');
		$this->assertEqual($map->getTargetNameFromPath('/test/kaboom/boom/'), 'Test5KaboomBoom');
		
		$this->assertEqual($map->getTargetNameFromPath('/test/kaboom/boom/doom'), 'Test1Wildcard');
		$this->assertEqual($map->getTargetNameFromPath('/test/kaboom/boom/doom/'), 'Test1Wildcard');
	}
	
	public function testURLValidation()	{
		$map = new URLMap();
		$this->assertTrue($map->isValidURL('/'));
		$this->assertTrue($map->isValidURL('/wibble'));
		$this->assertTrue($map->isValidURL('/wibble/'));
		$this->assertTrue($map->isValidURL('wibble/'));
		$this->assertTrue($map->isValidURL('/wibble/wobble/'));
		$this->assertTrue($map->isValidURL('/wobble/wibble'));
		$this->assertTrue($map->isValidURL('/wobble.wibble'));
		$this->assertTrue($map->isValidURL('/wobble.wibble*'));
		$this->assertTrue($map->isValidURL('/wobble.wibble/*'));
		$this->assertTrue($map->isValidURL('*'));
		
		$this->assertFalse($map->isValidURL('*/dsff/'));
		$this->assertFalse($map->isValidURL('wibble*/dsff/'));
	}
}
?>